package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlaverMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhaoxw
 * @description TODO
 * @date 2024/2/5 15:55
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishFlaverMapper dishFlaverMapper;

    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        long id = dish.getId();
//        DishFlavor dishFlavor = new DishFlavor();
        List<DishFlavor> list1 = dishDTO.getFlavors();
        if (null != list1 && list1.size() != 0) {
            for (DishFlavor flavor : list1) {
                flavor.setDishId(id);
            }
            dishFlaverMapper.insertBatch(list1);
        }

    }

    @Override
    public PageResult<DishVO> page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPageSize(), dishPageQueryDTO.getPage());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        PageResult pageResult1 = new PageResult();
        pageResult1.setTotal(total);
        pageResult1.setRecords(result);
        return pageResult1;


    }

    @Override
    public void delByIds(List<Long> ids) {
        // 起售中的菜品不能删除
        List<Dish> dishList = dishMapper.selectByIds(ids);
        for (Dish dish : dishList) {
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //被套餐关联的菜品不能删除
        List<SetmealDish> setmealDishList = setmealDishMapper.selectByIds(ids);
        if (setmealDishList != null && setmealDishList.size() != 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //可以正常删除，先删除dish表 再删除关联表dish_flavor
        dishMapper.delByIds(ids);
        dishFlaverMapper.delByDishIds(ids);

    }

    @Override
    public DishVO queryById(Long id) {
        Dish dish = dishMapper.queryById(id);
        List<DishFlavor> dishFlavorList = dishFlaverMapper.queryByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);
        dishMapper.updateWithFlavor(dish);
        Long id = dish.getId();
        //关联表的更新，先删除关联表 再插入关联表
        dishFlaverMapper.delByDishIds(Arrays.asList(id));

        List<DishFlavor> list1 = dishVO.getFlavors();
        if (null != list1 && list1.size() != 0) {
            for (DishFlavor flavor : list1) {
                flavor.setDishId(id);
            }
            dishFlaverMapper.insertBatch(list1);
        }


    }

    @Override
    public DishVO queryByIdz(long id) {
        //查询一条记录 封装成一个对象
        Dish dish = dishMapper.queryByIdz(id);
        //查询多个记录 必然返回List集合 一条记录对应一个对象 必然封装成List<T>泛型
        List<DishFlavor> dishFlavorList = dishFlaverMapper.queryByDishIdz(id);
        //将以上2个记录的返回结果封装成给前台返回的VO对象
        DishVO dishVO=new DishVO();
        //把dish里面的属性拷贝给dishVO
        BeanUtils.copyProperties(dish,dishVO);
        //再把剩下的属性赋值给dishVO
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }


}
